#!/bin/bash

set -e

## Demo: risk_version_list=("appv-aaa" "appv-bbb" "appv-ccc")
risk_version_list=()


user_list=$1
if [[ $user_list == "" ]];
then
  echo "Usage: ./check-cluster-node-num.sh \"'usr-xxx'\""
  echo "       lots of users use: \"'usr-xxx','usr-yyy'\""
  exit 1
fi

shell_pwd=$(cd "$( dirname "$0" )" && pwd)
if [ ! -d "${shell_pwd}/logs" ];
then
  mkdir ${shell_pwd}/logs
fi
log_date=$(date +'%Y%m%d-%H-%M-%S')
sql_cli="/pitrix/bin/exec_sql.py -d zone -c"
get_clster_sql="select cluster_id,root_user_id,app_id,app_version,name from cluster where root_user_id in (${user_list}) and status='active' and app_id!='app-qingcloud-appcenter-metadata'"
${sql_cli} "${get_clster_sql}" 2>/dev/null|grep 'cl-' >${shell_pwd}/logs/checkNum_allCluster.txt
cluster_zone=($(awk '{print $2}' ${shell_pwd}/logs/checkNum_allCluster.txt))
user_zone=($(awk '{print $4}' ${shell_pwd}/logs/checkNum_allCluster.txt))
app_zone=($(awk '{print $6}' ${shell_pwd}/logs/checkNum_allCluster.txt))
version_zone=($(awk '{print $8}' ${shell_pwd}/logs/checkNum_allCluster.txt))
name_zone=($(awk '{print $10}' ${shell_pwd}/logs/checkNum_allCluster.txt))
num=$(wc -l < ${shell_pwd}/logs/checkNum_allCluster.txt)
echo "集群名称 集群ID 应用ID 用户ID 节点角色 节点数量 是否高可用 是否风险版本" >${shell_pwd}/logs/check_cluster_num.log.${log_date}

for ((times=0;times<${num};times++));
do
  cluster_id=${cluster_zone[$times]}
  user_id=${user_zone[$times]}
  app_id=${app_zone[$times]}
  version_id=${version_zone[$times]}
  cluster_name=${name_zone[$times]}
  if [[ $app_id == "app-p6au3oyq" ]];  ## elk
  then
    if echo ${risk_version_list[*]} | grep -wq "${version_id}";
    then
      check_risk=${version_id}
    else
      check_risk="no"
    fi
    count_sql="select count(node_id) from cluster_node where cluster_id='${cluster_id}' and status='active' and role like 'es_node%';"
    node_num=$(${sql_cli} "${count_sql}" 2>/dev/null|grep -v -E "count|-"|awk '{print $2}')
    if [[ $node_num -lt 2 ]];
    then
      echo "${cluster_name} ${cluster_id} ${app_id} ${user_id} es_node ${node_num} no ${check_risk}" >>${shell_pwd}/logs/check_cluster_num.log.${log_date}
    else
      echo "${cluster_name} ${cluster_id} ${app_id} ${user_id} es_node ${node_num} yes ${check_risk}" >>${shell_pwd}/logs/check_cluster_num.log.${log_date}
    fi
  elif [[ $app_id == "app-00r26u27" ]];  ## mysql plus
  then
    if echo ${risk_version_list[*]} | grep -wq "${version_id}";
    then
      check_risk=${version_id}
    else
      check_risk="no"
    fi
    count_sql="select count(node_id) from cluster_node where cluster_id='${cluster_id}' and status='active' and role!='proxy' and role!='readinstance' and role!='analysis';"
    node_num=$(${sql_cli} "${count_sql}" 2>/dev/null|grep -v -E "count|-"|awk '{print $2}')
    if [[ $node_num -lt 3 ]];
    then
      echo "${cluster_name} ${cluster_id} ${app_id} ${user_id} -- ${node_num} no ${check_risk}" >>${shell_pwd}/logs/check_cluster_num.log.${log_date}
    else
      echo "${cluster_name} ${cluster_id} ${app_id} ${user_id} -- ${node_num} yes ${check_risk}" >>${shell_pwd}/logs/check_cluster_num.log.${log_date}
    fi
  elif [[ $app_id == "app-fdyvu2wk" ]];  ## etcd
  then
    if echo ${risk_version_list[*]} | grep -wq "${version_id}";
    then
      check_risk=${version_id}
    else
      check_risk="no"
    fi
    count_sql="select count(node_id) from cluster_node where cluster_id='${cluster_id}' and status='active' and role='etcd_node';"
    node_num=$(${sql_cli} "${count_sql}" 2>/dev/null|grep -v -E "count|-"|awk '{print $2}')
    if [[ $node_num -lt 3 ]];
    then
      echo "${cluster_name} ${cluster_id} ${app_id} ${user_id} etcd_node ${node_num} no ${check_risk}" >>${shell_pwd}/logs/check_cluster_num.log.${log_date}
    else
      echo "${cluster_name} ${cluster_id} ${app_id} ${user_id} etcd_node ${node_num} yes ${check_risk}" >>${shell_pwd}/logs/check_cluster_num.log.${log_date}
    fi
  elif [[ $app_id == "app-2mhyb1ui" ]];  ## harbor
  then
    if echo ${risk_version_list[*]} | grep -wq "${version_id}";
    then
      check_risk=${version_id}
    else
      check_risk="no"
    fi
    count_sql="select count(node_id) from cluster_node where cluster_id='${cluster_id}' and status='active' and role='web_node';"
    node_num=$(${sql_cli} "${count_sql}" 2>/dev/null|grep -v -E "count|-"|awk '{print $2}')
    if [[ $node_num -lt 2 ]];
    then
      echo "${cluster_name} ${cluster_id} ${app_id} ${user_id} web_node ${node_num} no ${check_risk}" >>${shell_pwd}/logs/check_cluster_num.log.${log_date}
    else
      echo "${cluster_name} ${cluster_id} ${app_id} ${user_id} web_node ${node_num} yes ${check_risk}" >>${shell_pwd}/logs/check_cluster_num.log.${log_date}
    fi
  elif [[ $app_id == "app-n9ro0xcp" ]];  ## kafka
  then
    if echo ${risk_version_list[*]} | grep -wq "${version_id}";
    then
      check_risk=${version_id}
    else
      check_risk="no"
    fi
    count_sql="select count(node_id) from cluster_node where cluster_id='${cluster_id}' and status='active' and role='kafka';"
    node_num=$(${sql_cli} "${count_sql}" 2>/dev/null|grep -v -E "count|-"|awk '{print $2}')
    if [[ $node_num -lt 3 ]];
    then
      echo "${cluster_name} ${cluster_id} ${app_id} ${user_id} kafka ${node_num} no ${check_risk}" >>${shell_pwd}/logs/check_cluster_num.log.${log_date}
    else
      echo "${cluster_name} ${cluster_id} ${app_id} ${user_id} kafka ${node_num} yes ${check_risk}" >>${shell_pwd}/logs/check_cluster_num.log.${log_date}
    fi
  elif [[ $app_id == "app-o1rl71en" ]];  ## mongodb
  then
    if echo ${risk_version_list[*]} | grep -wq "${version_id}";
    then
      check_risk=${version_id}
    else
      check_risk="no"
    fi
    count_sql="select count(node_id) from cluster_node where cluster_id='${cluster_id}' and status='active';"
    node_num=$(${sql_cli} "${count_sql}" 2>/dev/null|grep -v -E "count|-"|awk '{print $2}')
    if [[ $node_num -lt 3 ]];
    then
      echo "${cluster_name} ${cluster_id} ${app_id} ${user_id} -- ${node_num} no ${check_risk}" >>${shell_pwd}/logs/check_cluster_num.log.${log_date}
    else
      echo "${cluster_name} ${cluster_id} ${app_id} ${user_id} -- ${node_num} yes ${check_risk}" >>${shell_pwd}/logs/check_cluster_num.log.${log_date}
    fi
  elif [[ $app_id == "app-gtusp816" ]];  ## postgresql
  then
    if echo ${risk_version_list[*]} | grep -wq "${version_id}";
    then
      check_risk=${version_id}
    else
      check_risk="no"
    fi
    count_sql="select count(node_id) from cluster_node where cluster_id='${cluster_id}' and status='active';"
    node_num=$(${sql_cli} "${count_sql}" 2>/dev/null|grep -v -E "count|-"|awk '{print $2}')
    if [[ $node_num -lt 2 ]];
    then
      echo "${cluster_name} ${cluster_id} ${app_id} ${user_id} -- ${node_num} no ${check_risk}" >>${shell_pwd}/logs/check_cluster_num.log.${log_date}
    else
      echo "${cluster_name} ${cluster_id} ${app_id} ${user_id} -- ${node_num} yes ${check_risk}" >>${shell_pwd}/logs/check_cluster_num.log.${log_date}
    fi
  elif [[ $app_id == "app-n28v5lhg" ]];  ## rabbimq
  then
    if echo ${risk_version_list[*]} | grep -wq "${version_id}";
    then
      check_risk=${version_id}
    else
      check_risk="no"
    fi
    count_sql="select count(node_id) from cluster_node where cluster_id='${cluster_id}' and status='active' and (role like 'ram%' or role like 'disc%');"
    node_num=$(${sql_cli} "${count_sql}" 2>/dev/null|grep -v -E "count|-"|awk '{print $2}')
    if [[ $node_num -lt 3 ]];
    then
      echo "${cluster_name} ${cluster_id} ${app_id} ${user_id} disc|ram ${node_num} no ${check_risk}" >>${shell_pwd}/logs/check_cluster_num.log.${log_date}
    else
      echo "${cluster_name} ${cluster_id} ${app_id} ${user_id} disc|ram ${node_num} yes ${check_risk}" >>${shell_pwd}/logs/check_cluster_num.log.${log_date}
    fi
    haproxy_count_sql="select count(node_id) from cluster_node where cluster_id='${cluster_id}' and status='active' and role='haproxy';"
    haproxy_node_num=$(${sql_cli} "${haproxy_count_sql}" 2>/dev/null|grep -v -E "count|-"|awk '{print $2}')
    if [[ $haproxy_node_num -lt 2 ]];
    then
      echo "${cluster_name} ${cluster_id} ${app_id} ${user_id} haproxy ${haproxy_node_num} no ${check_risk}" >>${shell_pwd}/logs/check_cluster_num.log.${log_date}
    else
      echo "${cluster_name} ${cluster_id} ${app_id} ${user_id} haproxy ${haproxy_node_num} yes ${check_risk}" >>${shell_pwd}/logs/check_cluster_num.log.${log_date}
    fi
  elif [[ $app_id == "app-j8lnxify" ]];  ## rocketmq
  then
    if echo ${risk_version_list[*]} | grep -wq "${version_id}";
    then
      check_risk=${version_id}
    else
      check_risk="no"
    fi
    count_sql="select count(node_id) from cluster_node where cluster_id='${cluster_id}' and status='active' and role='broker';"
    node_num=$(${sql_cli} "${count_sql}" 2>/dev/null|grep -v -E "count|-"|awk '{print $2}')
    if [[ $node_num -lt 2 ]];
    then
      echo "${cluster_name} ${cluster_id} ${app_id} ${user_id} broker ${node_num} no ${check_risk}" >>${shell_pwd}/logs/check_cluster_num.log.${log_date}
    else
      echo "${cluster_name} ${cluster_id} ${app_id} ${user_id} broker ${node_num} yes ${check_risk}" >>${shell_pwd}/logs/check_cluster_num.log.${log_date}
    fi
    replica_count_sql="select count(node_id) from cluster_node where cluster_id='${cluster_id}' and status='active' and role='broker-replica';"
    replica_node_num=$(${sql_cli} "${replica_count_sql}" 2>/dev/null|grep -v -E "count|-"|awk '{print $2}')
    if [[ $replica_node_num -lt 1 ]];
    then
      echo "${cluster_name} ${cluster_id} ${app_id} ${user_id} broker-replica ${replica_node_num} no ${check_risk}" >>${shell_pwd}/logs/check_cluster_num.log.${log_date}
    else
      echo "${cluster_name} ${cluster_id} ${app_id} ${user_id} broker-replica ${replica_node_num} yes ${check_risk}" >>${shell_pwd}/logs/check_cluster_num.log.${log_date}
    fi
    ns_count_sql="select count(node_id) from cluster_node where cluster_id='${cluster_id}' and status='active' and role='nameserver';"
    ns_node_num=$(${sql_cli} "${ns_count_sql}" 2>/dev/null|grep -v -E "count|-"|awk '{print $2}')
    if [[ $ns_node_num -lt 2 ]];
    then
      echo "${cluster_name} ${cluster_id} ${app_id} ${user_id} nameserver ${ns_node_num} no ${check_risk}" >>${shell_pwd}/logs/check_cluster_num.log.${log_date}
    else
      echo "${cluster_name} ${cluster_id} ${app_id} ${user_id} nameserver ${ns_node_num} yes ${check_risk}" >>${shell_pwd}/logs/check_cluster_num.log.${log_date}
    fi
  elif [[ $app_id == "app-zydumbxo" ]];  ## redis standalone
  then
    if echo ${risk_version_list[*]} | grep -wq "${version_id}";
    then
      check_risk=${version_id}
    else
      check_risk="no"
    fi
    count_sql="select count(node_id) from cluster_node where cluster_id='${cluster_id}' and status='active';"
    node_num=$(${sql_cli} "${count_sql}" 2>/dev/null|grep -v -E "count|-"|awk '{print $2}')
    if [[ $node_num -lt 3 ]];
    then
      echo "${cluster_name} ${cluster_id} ${app_id} ${user_id} -- ${node_num} no ${check_risk}" >>${shell_pwd}/logs/check_cluster_num.log.${log_date}
    else
      echo "${cluster_name} ${cluster_id} ${app_id} ${user_id} -- ${node_num} yes ${check_risk}" >>${shell_pwd}/logs/check_cluster_num.log.${log_date}
    fi
  elif [[ $app_id == "app-y6i338bf" ]];  ## redis cluster
  then
    if echo ${risk_version_list[*]} | grep -wq "${version_id}";
    then
      check_risk=${version_id}
    else
      check_risk="no"
    fi
    count_sql="select count(node_id) from cluster_node where cluster_id='${cluster_id}' and status='active' and role='master';"
    node_num=$(${sql_cli} "${count_sql}" 2>/dev/null|grep -v -E "count|-"|awk '{print $2}')
    if [[ $node_num -lt 3 ]];
    then
      echo "${cluster_name} ${cluster_id} ${app_id} ${user_id} master ${node_num} no ${check_risk}" >>${shell_pwd}/logs/check_cluster_num.log.${log_date}
    else
      echo "${cluster_name} ${cluster_id} ${app_id} ${user_id} master ${node_num} yes ${check_risk}" >>${shell_pwd}/logs/check_cluster_num.log.${log_date}
    fi
    replica_count_sql="select count(node_id) from cluster_node where cluster_id='${cluster_id}' and status='active' and role='master-replica';"
    replica_node_num=$(${sql_cli} "${replica_count_sql}" 2>/dev/null|grep -v -E "count|-"|awk '{print $2}')
    if [[ $replica_node_num -lt 1 ]];
    then
      echo "${cluster_name} ${cluster_id} ${app_id} ${user_id} master-replica ${replica_node_num} no ${check_risk}" >>${shell_pwd}/logs/check_cluster_num.log.${log_date}
    else
      echo "${cluster_name} ${cluster_id} ${app_id} ${user_id} master-replica ${replica_node_num} yes ${check_risk}" >>${shell_pwd}/logs/check_cluster_num.log.${log_date}
    fi
  elif [[ $app_id == "app-tg3lbp0a" ]];  ## zookeeper
  then
    if echo ${risk_version_list[*]} | grep -wq "${version_id}";
    then
      check_risk=${version_id}
    else
      check_risk="no"
    fi
    count_sql="select count(node_id) from cluster_node where cluster_id='${cluster_id}' and status='active';"
    node_num=$(${sql_cli} "${count_sql}" 2>/dev/null|grep -v -E "count|-"|awk '{print $2}')
    if [[ $node_num -lt 3 ]];
    then
      echo "${cluster_name} ${cluster_id} ${app_id} ${user_id} -- ${node_num} no ${check_risk}" >>${shell_pwd}/logs/check_cluster_num.log.${log_date}
    else
      echo "${cluster_name} ${cluster_id} ${app_id} ${user_id} -- ${node_num} yes ${check_risk}" >>${shell_pwd}/logs/check_cluster_num.log.${log_date}
    fi
  else
    if echo ${risk_version_list[*]} | grep -wq "${version_id}";
    then
      check_risk=${version_id}
    else
      check_risk="no"
    fi
    count_sql="select count(node_id) from cluster_node where cluster_id='${cluster_id}' and status='active' and role!='client';"
    node_num=$(${sql_cli} "${count_sql}" 2>/dev/null|grep -v -E "count|-"|awk '{print $2}')
    if [[ $node_num -lt 2 ]];
    then
      echo "${cluster_name} ${cluster_id} ${app_id} ${user_id} -- ${node_num} no ${check_risk}" >>${shell_pwd}/logs/check_cluster_num.log.${log_date}
    else
      echo "${cluster_name} ${cluster_id} ${app_id} ${user_id} -- ${node_num} yes ${check_risk}" >>${shell_pwd}/logs/check_cluster_num.log.${log_date}
    fi
  fi

done

column -t < ${shell_pwd}/logs/check_cluster_num.log.${log_date}
